import 'package:flutter/material.dart';
import 'package:note/tool/eventBus.dart';
import '../ViewModel/login_vm.dart';
import 'package:provider/provider.dart';



class LoginPage extends StatefulWidget {
  const LoginPage({ Key? key }) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  late TextEditingController _accountNumber ;
  late TextEditingController _scureText ; 
  late String _textAccount ;
  late String _textScure;
  @override
  // ignore: must_call_super
  void initState() {
    // TODO: implement initState
    _accountNumber =new TextEditingController();
    _scureText = new TextEditingController();
    _textAccount ='';
    _textScure='';
    eventBus.on<EventLogin>().listen((event){
      print('监听事件为：${event.obj}');
    });
    
    
  } 
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _accountNumber.dispose();
    _scureText.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body:Container(
           decoration: BoxDecoration(
            image: DecorationImage(
                  image: ExactAssetImage('images/bg_light.jpg'),
                  alignment: Alignment.topCenter,
                  fit: BoxFit.fill,
                ),
           
            ),
       child:Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          children: [
            Image.asset('images/head.png',width: double.infinity,fit: BoxFit.fill),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Expanded(child: TextField(
                  onChanged: (value){
                    setState(() {
                      _textAccount =value;
                    });
                  },
              maxLength: 11,
              controller: _accountNumber,
              textInputAction: TextInputAction.next,
              decoration: InputDecoration(
                labelText: '账号',
              ),
            ),),
             Offstage(offstage: _textAccount ==''?true:false,
            child:IconButton(onPressed: (){
              setState(() {
               _textAccount = ''; 
               _accountNumber.clear();
              });
            },
             icon: Icon(Icons.close,color: Colors.grey,size: 17)) ,
            )  
            /* 
                 Offstage(
              offstage: _textAccount == ''?true:false,
              child: GestureDetector(
                onTap: (){
                  setState(() {
                                            _textAccount = ''; 
                                            _accountNumber.clear(); 
                                              //清除textfield的值
                                          });
                                        },
                                        child: Container(
                                            width: 45,
                                            height: 45,
                                            child: Icon(Icons.close,color: Colors.grey,size: 17,),
                                        ),
                                                                    
                                  ),
            )*/
              ],
            ),
             Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Expanded(child: 
              TextField(
                onChanged: (value){
                    setState(() {
                      _textScure =value;
                    });
                  },
              maxLength: 8,
              controller: _scureText,
              textInputAction: TextInputAction.send,
              obscureText: true,
              decoration: InputDecoration(
                labelText: '密码',
              ),
            ),),
         /* 
            Offstage(
              offstage: _textScure ==''?true:false,
              child: GestureDetector(
                onTap: (){
                  setState(() {
                                            _textScure = ''; 
                                            _scureText.clear(); 
                                              //清除textfield的值
                                          });
                                        },
                                        child: Container(
                                            width: 45,
                                            height: 45,
                                            child: Icon(Icons.close,color: Colors.grey,size: 17,),
                                        ),
                                                                    
                                  ),
            )
            */
            Offstage(offstage: _textScure ==''?true:false,
            child:IconButton(onPressed: (){
              setState(() {
                _textScure = '';
                _scureText.clear();
              });
            },
             icon: Icon(Icons.close,color: Colors.grey,size: 17)) ,
            )
            
              ],
            ),
            Container(
              width: double.infinity,
              height: 40,
            
              child:ElevatedButton(
                onPressed: _login, child: Text('登录')) ,)
            
            
            
             ])

    ))
    );
  
}
void _login()async{
    context.read<login_vm>().login(_textAccount, _textScure);
}
_showLoginError(){
  showDialog(context: context, builder: (context){
    return AlertDialog(
      title: Text('账号或密码错误')
      
    );
  });
}
_showLoginAlert(){
  showDialog(context: context, builder: (context){
    return AlertDialog(
      title: Text('登陆成功'),
      content: Text('您的登录账号是：${_accountNumber.text}\n密码是：\n${_scureText.text}'),
    );
  });
}




}